package logic

import (
	"context"
	"errors"
	"goods-login/goods/goods"
	"goods-login/goods/internal/svc"
	"goods-login/kpg"
	"goods-login/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *goods.LoginRequest) (*goods.LoginResponse, error) {
	// todo: add your logic here and delete this line
	data := mysql.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	//Captcha1, _ := cache.Rdb.Get(l.ctx, "Captcha"+in.Mobile).Result()
	//if in.Captcha != Captcha1 {
	//	return nil, errors.New("图形验证码错误")
	//}
	//SendSms1, _ := cache.Rdb.Get(l.ctx, "SendSms"+in.Mobile).Result()
	//if in.SendSms != SendSms1 {
	//	return nil, errors.New("短信验证码错误")
	//}
	if data.Password != kpg.Md5(in.Password) {
		return nil, errors.New("密码错误,请修改")
	}
	data.Password = kpg.Md5(in.Password)
	err := data.LoginUser(mysql.DB, in.Mobile)
	if err != nil {
		return nil, err
	}
	return &goods.LoginResponse{
		UserId: int64(data.ID),
	}, nil
}
